package cn.schoolwow.workflow.test.module.oa.general.testcase;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.dto.FlowContextDataRange;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.QuickWorkFlow;
import cn.schoolwow.workflow.domain.instance.StartWorkFlowRequest;
import cn.schoolwow.workflow.domain.instance.WorkFlowInstanceQuery;
import cn.schoolwow.workflow.domain.instance.WorkFlowInstanceStatus;
import cn.schoolwow.workflow.domain.task.TaskType;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskQuery;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskResponse;
import cn.schoolwow.workflow.entity.WorkFlowInstance;
import cn.schoolwow.workflow.test.module.oa.common.flow.DeployConvertWorkFlowFlow;
import org.apache.commons.lang3.Validate;

public class StartConvertContractAuditTestCaseFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        deployWorkFlow(flowContext);
        startWorkFlow(flowContext);
        audit(flowContext);
        checkCarbonCopy(flowContext);
    }

    @Override
    public String name() {
        return "转换原始数据-启动合同审批工作流";
    }

    private void deployWorkFlow(FlowContext flowContext){
        String deployName = "合同审批转换工作流";
        flowContext.putData("deployName", deployName);

        flowContext.startFlow(new DeployConvertWorkFlowFlow())
                .putCurrentCompositeFlowData("deployPath", "oa/合同审批原始数据.json")
                .execute();
    }

    private void startWorkFlow(FlowContext flowContext){
        String deployName = flowContext.checkData("deployName", String.class);
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);

        StartWorkFlowRequest startWorkFlowRequest = new StartWorkFlowRequest();
        startWorkFlowRequest.definitionName = deployName;
        startWorkFlowRequest.initiator = "张三";
        flowContext.putInstanceData(startWorkFlowRequest, FlowContextDataRange.ContextThreadLocal);

        WorkFlowInstance workFlowInstance = quickWorkFlow.startWorkFlow(startWorkFlowRequest);
        flowContext.putInstanceData(workFlowInstance);
    }

    private void audit(FlowContext flowContext){
        WorkFlowInstance workFlowInstance = flowContext.checkInstanceData(WorkFlowInstance.class);
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);

        String[] auditors = new String[]{"0:1","0:2"};
        WorkFlowTaskQuery workFlowTaskQuery = new WorkFlowTaskQuery();
        workFlowTaskQuery.instanceIdList.add(workFlowInstance.getId());
        workFlowTaskQuery.instanceStatusList.add(WorkFlowInstanceStatus.Running.status);
        for(String auditor:auditors){
            workFlowTaskQuery.assignerList.clear();
            workFlowTaskQuery.assignerList.add(auditor);
            WorkFlowTaskResponse workFlowTaskResponse = quickWorkFlow.getSingleWorkFlowTask(workFlowTaskQuery);
            quickWorkFlow.completeTask(workFlowTaskResponse.taskId, TaskType.COMPLETE);
        }
    }

    private void checkCarbonCopy(FlowContext flowContext){
        WorkFlowInstance workFlowInstance = flowContext.checkInstanceData(WorkFlowInstance.class);
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);

        WorkFlowInstanceQuery workFlowInstanceQuery = new WorkFlowInstanceQuery();
        workFlowInstanceQuery.instanceIdList.add(workFlowInstance.getId());
        workFlowInstanceQuery.carbonCopyList.add("0:1");
        WorkFlowInstance carbonCopyWorkFlowInstance = quickWorkFlow.getSingleWorkFlowInstance(workFlowInstanceQuery);
        Validate.notNull(carbonCopyWorkFlowInstance, "获取抄送给指定人员的实例失败");
    }

}
